package com.huayun.base;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


import com.huayun.inter_face.Action;
import com.huayun.manager.AppManager;
import com.huayun.statusbar.AndroidBug;
import com.huayun.statusbar.StatusBarUtil;
import com.huayun.viewutils.imageutil.ScreenUtils;

import butterknife.ButterKnife;

/**
 * Description:
 * Created on 2019/8/14 0014 15:06:53
 * Organization:华云
 * author:AHuangSHang
 */
public abstract class BaseActivity extends AppCompatActivity implements IBaseActivity, Action<Object> {


    public Context c;// 环境变量
    /**
     * Material Design风格Toolbar控件
     */
    private Toolbar toolbar;
    public TextView toolbarTitle;
    public ImageView toolbarBack, toolbarMore;
    private int systemType;//系统类型 1:MIUI 2:Flyme 3:android6.0
    private View toolBarMoreLayout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        c = this;
        systemType = StatusBarUtil.StatusBarDarkMode(this);
        StatusBarUtil.statusBarColor(this);
        StatusBarUtil.StatusBarDarkMode(this, systemType);
        AppManager.getAppManager().addActivity(this);
        if (getContentViewLayoutResId() > 0) {
            View layoutView = LayoutInflater.from(this).inflate(getContentViewLayoutResId(), null);
            setContentView(layoutView);
            ButterKnife.bind(this);
            //获取到状态栏的高度
            int statusHeight = ScreenUtils.getStatusBarHeight(this);
            View v = findViewById(getToolbarResId());
            if (v != null) {
                toolbar = (Toolbar) v;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    toolbar.setPadding(0, statusHeight, 0, 0);
                }
                setSupportActionBar(toolbar);
                toolbarTitle = (TextView) v.findViewById(R.id.toolbar_title);
                toolbarBack = v.findViewById(R.id.toolbar_back);
                toolbarMore = v.findViewById(R.id.toolbar_more);
                toolBarMoreLayout = v.findViewById(R.id.toolbar_more_layout);
                toolbar.findViewById(R.id.toolbar_back_layout).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onBackKeyClick();
                    }
                });
                toolbarTitle.setMaxWidth((int) (ScreenUtils.getScreenWith(this) * 0.5));
                if (toolbarTitle != null && getSupportActionBar() != null) {
                    getSupportActionBar().setDisplayShowTitleEnabled(false);
                }
            }
        }
        AndroidBug.assistActivity(this,this);
        initData(savedInstanceState);
        initView();
        bindView();
    }

    protected abstract void bindView();

    protected abstract void initView();

    protected abstract void initData(@Nullable Bundle savedInstanceState);

    protected abstract int getContentViewLayoutResId();

    @Override
    public int getToolbarResId() {
        return R.id.toolbar;
    }

    public void setToolbarTitle(String title) {
        if (toolbarTitle != null) {
            toolbarTitle.setText(title);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onBackKeyClick();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void startActivity(Intent intent) {
        super.startActivity(intent);
        overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
    }

    public void startActivityForResult(Intent intent, int requestCode) {
        super.startActivityForResult(intent, requestCode);
        overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
    }

    @SuppressLint("RestrictedApi")
    public void startActivityForResult(Intent intent, int requestCode, @Nullable Bundle options) {
        super.startActivityForResult(intent, requestCode, options);
    }

    public void setTitle(String title) {
        setTitle(title, -1, -1, false, null, null);

    }

    /**
     * set toolBarTitle
     *
     * @param title             title
     * @param backResId         left icon image resource
     * @param moreResId         right icon image resource
     * @param showMore          need show right icon
     * @param moreClickListener right icon click listener
     */
    public void setTitle(String title, int backResId, int moreResId, boolean showMore, final Action<View> backClickListener, final Action<View> moreClickListener) {
        if (toolbarTitle != null) {
            toolbarTitle.setText(title);
        }
        if (toolbarBack != null) {
            if (backResId != -1) {
                toolbarBack.setImageResource(backResId);
            }
            toolbar.findViewById(R.id.toolbar_back_layout).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (backClickListener != null) {
                        backClickListener.call(v);
                    } else {
                        onBackKeyClick();
                    }
                }
            });
        }
        if (toolbarMore != null) {
            if (moreResId != -1) {
                toolbarMore.setImageResource(moreResId);
            }

        }
        if (toolBarMoreLayout != null) {
            toolBarMoreLayout.setVisibility(showMore ? View.VISIBLE : View.GONE);

            toolBarMoreLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (moreClickListener != null) {
                        moreClickListener.call(v);
                    }
                }
            });
        }
    }

    public void onBackKeyClick() {
        AppManager.getAppManager().finishActivity(this);
    }

    public int getSystemType() {
        return systemType;
    }

    public Toolbar getToolbar() {
        return toolbar;
    }

    //更改系统状态栏字体为黑色
    public void setStatusBarLightModel() {
        StatusBarUtil.StatusBarLightMode(this, systemType);
    }

    @Override
    public void call(Object o) {

    }
}
